package com.lyc.mapper;

import com.lyc.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface UserMapper {
    // 根据用户名查询用户
    @Select("select * from users where username = #{username}")
    User findByUserName(String username);

    // 添加注册用户
    @Insert("insert into users(username,password,avatar) values(#{username},#{md5String},#{avatar})")
    void add(String username, String md5String, String avatar);

    // 通过id查询用户
    @Select("select * from users where id = #{userId}")
    User findById(Integer userId);

    // 根据id更新用户信息
    @Update("update users set username = #{username},  bio = #{bio}, email = #{email}  where id = #{id}")
    void updateById(User user);

    // 根据id更新用户信息（管理员）
    @Update("update users set username = #{username}, password = #{password}, nickname = #{nickname}," +
            " bio = #{bio}, email = #{email}, student_id = #{studentId}, " +
            "is_verified = #{isVerified}, role = #{role}, status = #{status} " +
            "where id = #{id}")
    void updateUserInfoByAdmin(User user);

    // 获取用户帖子数量
    @Select("select count(*) from posts where user_id = #{userId}")
    Integer getPostCount(Integer userId);

    // 更新用户头像
    @Update("update users set avatar = #{avatar} where id = #{id}")
    void updateAvatar(String avatar, Integer id);

    // 更新用户信息(支持部分更新)
    @Update("update users set nickname = #{nickname},bio = #{bio},phone = #{phone},email = #{email} where id = #{id}")
    void updateUserInfo(User user);

    // 根据followerId查询用户
    @Select("select * from users where id = #{followerId}")
    User selectById(Integer followerId);

    // 更新用户关注数
    @Update("update users set following_count = #{followingCount} where id = #{id}")
    void updateFollowingCount(User user);

    // 更新用户粉丝数
    @Update("update users set followers_count = #{followersCount} where id = #{id}")
    void updateFollowersCount(User user);
    
    // 获取全部用户列表，管理员排在前面
    @Select("select * from users order by case when role = 'admin' then 0 else 1 end, id")
    List<User> getAllUser();
    
    // 更新用户状态
    @Update("update users set status = #{status} where id = #{id}")
    void updateUserStatus(User user);
    
    // 更新用户角色
    @Update("update users set role = #{role} where id = #{id}")
    void updateUserRole(User user);
    
    // 更新用户认证状态
    @Update("update users set is_verified = #{isVerified} where id = #{id}")
    void updateUserVerification(User user);
    
    // 根据ID删除用户
    @Update("delete from users where id = #{id}")
    void deleteUserById(Integer id);
}